package com.ycl.controller;

import com.baomidou.kaptcha.Kaptcha;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.ycl.constants.Jurisdiction;
import com.ycl.pojo.User;
import com.ycl.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.TimeUnit;


@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;//数据库
    @Autowired
    RedisTemplate redisTemplate;//redis(非关系型数据库)用来存储验证码
    @Autowired
    private Kaptcha kaptcha;//验证码生成 使用的是Lombok

    @PostMapping("/login")
    public void  login( String phone,String password,@RequestParam("captcha") String code, HttpSession session, HttpServletResponse resp) throws IOException {
        // 从redis中获取缓存的验证码
        String codeInSession = (String) redisTemplate.opsForValue().get(code);

        // 进行验证码比对
        if (codeInSession != null && codeInSession.equals(code)){
            // 比对成功
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(queryWrapper);

            if (user!=null && user.getPassword().equals(password)){
                session.setAttribute("user",user.getId());
                // 如果用户登录成功删除redis中缓存的验证码
                redisTemplate.delete(code);
                if (user.getJurisdiction().equals(Jurisdiction.JURISDICTION_ADMIN)){
                    resp.sendRedirect("/index.html");
                    return;
                }

                resp.sendRedirect("/indexAdmin.html");
                return ;
            }
        }
        // 比对失败
        resp.sendRedirect("/fail.html");
    }


    @PostMapping("/register")
    public void  register(String phone,String email,String password,String confirmPassword,String captcha,HttpServletResponse resp) throws IOException {

        System.out.println(captcha);
        // 从redis中获取缓存的验证码
        String codeInSession = (String) redisTemplate.opsForValue().get(captcha);

        if (codeInSession !=null && codeInSession.equals(captcha)
                && phone != null && password != null && password.equals(confirmPassword)){
            User user = new User();
            user.setPhone(phone);
            user.setPassword(password);
            user.setEmail(email);
            user.setJurisdiction(Jurisdiction.JURISDICTION_COMMON);
            redisTemplate.delete(captcha);
            userService.save(user);
            resp.sendRedirect("/success.html");
            return;
        }
        // 比对失败
        resp.sendRedirect("/fail.html");
    }

    //生成验证码
    @GetMapping("/captcha")
    public void render() {
        String render = kaptcha.render();
        redisTemplate.opsForValue().set(render,render,5, TimeUnit.MINUTES);
    }

    //校验验证码
    @PostMapping("/captcha/valid")
    public void validDefaultTime(@RequestParam String code) {
        //default timeout 900 seconds
        kaptcha.validate(code);
    }

    //过期时间
    @PostMapping("/captcha/validTime")
    public void validWithTime(@RequestParam String code) {
        kaptcha.validate(code, 60);
    }
}